﻿Imports TNCDAL
Imports TNCBAL
Imports HTLibrary
Public Class ucCountries
    Private ctlCountry As New CCountryController
    Private currCountry As tblCountry
    Private sSearch As String = ""

    Private Sub ucCountries_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'hiển thị danh sách các nước
        BindGridCountries()
        'hien thi nut them, xoa, sua, thoat
        HienThiNut_Thuong()
    End Sub
#Region "dgCountries Event"
    'Private Sub dgCountries_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgCountries.CellBeginEdit
    '    If Not dgCountries.Rows(e.RowIndex).IsNewRow Then
    '        If e.RowIndex < dgCountries.NewRowIndex Then
    '            If AddedRowIndex <> e.RowIndex Then
    '                e.Cancel = True
    '            End If
    '        End If
    '    End If
    'End Sub

    'Private Sub dgCountries_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgCountries.CellEndEdit
    '    If (String.IsNullOrEmpty(dgCountries.Rows(e.RowIndex).ErrorText)) Then
    '        arrIndex.Add(e.RowIndex)
    '    Else
    '        dgCountries.Rows(e.RowIndex).ErrorText = String.Empty
    '    End If

    'End Sub 

    Private Sub dgCountries_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgCountries.SelectionChanged
        'Load info
        Try
            Dim id As Integer = dgCountries.CurrentRow.Cells("ID_Country").Value
            currCountry = ctlCountry.SelectRecordByID(id)

            'Kiểm tra nếu đang cập nhật/thêm mới thì không hiển thị dữ liệu
            If btnUpdate.Enabled Then
                Return
            End If

            'Fill company's fields
            FillCountryForm()

        Catch ex As Exception

        End Try
    End Sub

    'Private Sub dgCountries_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dgCountries.UserAddedRow
    '    AddedRowIndex = dgCountries.NewRowIndex - 1
    'End Sub
#End Region

#Region "Button Event"
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If (MessageBox.Show("Bạn có chắc muốn xoá " + currCountry.CountryName + " ?", "Xác nhận", MessageBoxButtons.YesNo) = vbYes) Then
            'xoa khoi du lieu bằng cách set IsActive = false
            currCountry.IsActive = False
            ctlCountry.UpdateRecordWithoutAttach(currCountry)
            'ghi log
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Delete, "Country: " & currCountry.CountryName)
            'load lại grid
            BindGridCountries()
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'kiểm tra có trùng tên
        If (txtCountryName.Text = "") Then
            MessageBox.Show("Country name is empty.")
            txtCountryName.Focus()
            Exit Sub
        End If
        Dim trungTen As Boolean = False
        'kiểm tra xem tên có bị trùng hay không
        If (ctlCountry.CheckCountryExists(txtCountryName.Text, currCountry.ID_Country)) Then
            MessageBox.Show("This country exists. Please enter another country name.")
            txtCountryName.Focus()
            Return
        End If

        'luu du lieu 
        With currCountry
            .CountryName = txtCountryName.Text
            .IsActive = True
        End With
        Dim idCurr As Integer = currCountry.ID_Country
        If currCountry.ID_Country > 0 Then
            ctlCountry.UpdateRecordWithoutAttach(currCountry)
            'ghi log
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, "Country: " & currCountry.CountryName)
        Else
            ctlCountry.InsertRecord(currCountry)
            idCurr = currCountry.ID_Country
            'ghi log
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, "Country: " & currCountry.CountryName)
        End If
        BindGridCountries()
        Dim indexRow As Integer = -1
        'lấy ra vị trí của port vừa thêm hoặc chỉnh sửa vào
        indexRow = FindIndexOfPortInGrid(idCurr)
        'Set current row
        dgCountries.CurrentCell = dgCountries.Rows(indexRow).Cells(1)
        dgCountries.Rows(indexRow).Selected = True
        'tro ve trạng thái thường
        HienThiNut_Thuong()
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'dgCountries.AllowUserToAddRows = True
        'dgCountries.ReadOnly = False
        txtCountryName.Enabled = True
        HienThiNut_Sua()
        RefreshForm()
        'Init obj
        currCountry = New tblCountry
        txtCountryName.Focus()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'dgCountries.ReadOnly = False
        'dgCountries.AllowUserToAddRows = False
        txtCountryName.Enabled = True
        HienThiNut_Sua()
        txtCountryName.Focus()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Dim _id As Integer = dgCountries.CurrentRow.Cells("ID_Country").Value
            currCountry = ctlCountry.SelectRecordByID(_id)
        Catch ex As Exception
        End Try
        FillCountryForm()
        'hien thi lai trang thai binh thuong
        HienThiNut_Thuong()
    End Sub
#End Region

#Region "Utilities"
    Private Function FindIndexOfPortInGrid(ByVal id As Integer) As Integer
        Dim index As Integer = 0
        For Each row As DataGridViewRow In dgCountries.Rows
            If (row.Cells("ID_Country").Value = id) Then
                index = row.Index
                Exit For
            End If
        Next
        Return index
    End Function
    Private Sub FillCountryForm()
        If (currCountry IsNot Nothing) Then
            txtCountryName.Text = currCountry.CountryName
        Else
            txtCountryName.Text = ""
        End If

    End Sub
    Public Sub RefreshForm()
        txtCountryName.Text = ""
    End Sub
    Private Function CheckNameExist(ByVal sName As String, ByVal index As Integer) As Boolean
        For Each row As DataGridViewRow In dgCountries.Rows
            If (row.Index <> index) Then
                Dim cName As String = row.Cells(1).Value.ToString
                If (sName.ToLower = cName.ToLower) Then
                    Return True
                End If
            End If
        Next
        Return False
    End Function
    Private Sub HienThiNut_Thuong()
        'dgCountries.AllowUserToAddRows = False
        'dgCountries.AllowUserToDeleteRows = False
        'dgCountries.ReadOnly = True
        txtCountryName.Enabled = False
        btnAdd.Enabled = True
        btnDel.Enabled = True
        btnEdit.Enabled = True
        btnUpdate.Enabled = False
        btnCancel.Enabled = False
        dgCountries.Enabled = True
        If (dgCountries.Rows.Count = 0) Then
            btnEdit.Enabled = False
            btnDel.Enabled = False
        End If
    End Sub
    Private Sub HienThiNut_Sua()
        dgCountries.Enabled = False
        txtCountryName.Enabled = True
        btnAdd.Enabled = False
        btnDel.Enabled = False
        btnEdit.Enabled = False
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
    End Sub
    Private Sub BindGridCountries()
        Dim countries = ctlCountry.SelectRecords(sSearch)
        Dim source As New BindingSource
        source.DataSource = countries
        dgCountries.DataSource = source
        BindingNavigator1.BindingSource = source
        With dgCountries
            .Columns("ID_Country").Visible = False
            .Columns("IsActive").Visible = False
            .Columns("CountryName").HeaderText = "Tên quốc gia"
            .Columns("CountryName").Width = dgCountries.Width
        End With
        If (dgCountries.Rows.Count = 0) Then
            btnEdit.Enabled = False
            btnDel.Enabled = False
            txtCountryName.Text = ""
        Else
            btnEdit.Enabled = True
            btnDel.Enabled = True
        End If
    End Sub
#End Region

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(dgCountries)
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtSearch.Text = ""
        sSearch = ""
        BindGridCountries()
        txtSearch.Focus()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        sSearch = txtSearch.Text
        BindGridCountries()
    End Sub

    Private Sub txtSearch_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyUp
        If (e.KeyCode = Keys.Enter) Then
            btnSearch_Click(Nothing, Nothing)
        End If
    End Sub
End Class
